﻿using NHibernate.Mapping.ByCode;
using NHibernate.Mapping.ByCode.Conformist;
using SellAndBuy.Application.Query.DTOs;

namespace SellAndBuy.Infrastructure.Data.Query.Mappings
{
    public class ItemDTOMap : ClassMapping<ItemDTO>
    {
        public ItemDTOMap()
        {
            Lazy(false);
            Table("Item");
            Id(x => x.ID, map => map.Generator(Generators.HighLow, gmap => gmap.Params(new {max_low = 100})));
            Property(x => x.Name, map => { map.Length(150); map.NotNullable(true); });
            Property(x => x.Description, map => { map.Length(1000); map.NotNullable(true); });
            ManyToOne(x => x.Category, map =>
                                           {
                                               map.Column("CategoryID");
                                               map.NotNullable(true); 
                                               map.Lazy(LazyRelation.NoProxy);
                                           });
            Bag(x => x.Pictures,
               collectionMapping =>
               {
                   collectionMapping.Table("ItemPictures");
                   collectionMapping.Access(Accessor.NoSetter);
                   collectionMapping.Cascade(Cascade.All);
                   collectionMapping.Key(k => k.Column("ItemID"));
                   collectionMapping.Lazy(CollectionLazy.NoLazy);
               },
               mapping => mapping.Component(PictureInfoDTOMap.Mapping()));
        }    
    }
}